package com.budwk.app.sys.services.impl;

import cn.hutool.core.text.StrFormatter;
import com.budwk.app.sqfw.dto.TransferServiceRecordDTO;
import com.budwk.app.sqfw.models.Sqfw_ImgCallback;
import com.budwk.app.sqfw.models.Sqfw_servicerecord;
import com.budwk.app.sqfw.models.Sqfw_workorder;
import com.budwk.app.sqfw.services.*;
import com.budwk.app.sqfw.tencent.TCyysb;
import com.budwk.app.sys.models.Sys_task;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysTaskService;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.database.service.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author wizzer@qq.com
 */
@IocBean(args = {"refer:dao"})
public class SysTaskServiceImpl extends BaseServiceImpl<Sys_task> implements SysTaskService {
    public SysTaskServiceImpl(Dao dao) {
        super(dao);
    }

    @Inject
    private HuaweiOssService huaweiOssService;

    @Inject
    private SqfwServicerecordService sqfwServicerecordService;

    @Inject
    private SysUserService sysUserService;

    @Inject
    private SqfwWorkorderService sqfwWorkorderService;

    @Inject
    private SqfwCopyrecordService sqfwCopyrecordService;

    @Inject
    private SqfwImgcallbackService sqfwImgcallbackService;

    @Inject
    private PropertiesProxy propertiesProxy;


    @Override
    public Result quartzCreateServiceRecord(Sqfw_servicerecord sqfwServicerecord, TransferServiceRecordDTO transferServiceRecordDTO, String ImgId) {
        sqfwServicerecord.setCreatedBy(sqfwServicerecord.getUser_id());
        String getPhone = sqfwServicerecord.getService_phone();
        if (StringUtils.isNotBlank(sqfwServicerecord.getRecording_file())
                && !sqfwServicerecord.getApp_type().equals("7")
                && !sqfwServicerecord.getApp_type().equals("8")) {
            String url = huaweiOssService.getPwdPath(sqfwServicerecord.getRecording_file());

            String serviceIp = propertiesProxy.get("jarService.ip");
            String port = propertiesProxy.get("server.port");
            String callback = StrFormatter.format("http://{}:{}/platform/sqfw/cloudcallback/callback", serviceIp, port);
            Long taskId = TCyysb.Send(url, callback);
            System.out.println("新增通讯服务记录: ");
            System.out.println(taskId);
            System.out.println(sqfwServicerecord);

            if (taskId != null && taskId > 0L) {
                sqfwServicerecord.setTaskId(taskId.toString());
            }
        }
        sqfwServicerecord.setDelFlag(false);
        if (!StringUtils.isNotBlank(sqfwServicerecord.getService_phone()) && sqfwServicerecord.getDuration() != null) {
            Long shichang = sqfwServicerecord.getDuration() / 1000;
            sqfwServicerecord.setDuration(shichang);
            Date endtime = new Date(sqfwServicerecord.getConnect_time().getTime() + shichang);
            sqfwServicerecord.setEnd_time(endtime);
        }
        sqfwServicerecord.setRecording_text(sqfwServicerecord.getRecording_text());
        //消息转发后自动会填
        sqfwServicerecord.setService_phone(null);
        sqfwServicerecord.setService_name(null);
        sqfwServicerecord.setFinishStatus("new");
       /* if (sqfwServicerecord.getType().equals("安全排查整治")||sqfwServicerecord.getType().equals("日报")){
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("MM-dd");
            String timeStr=simpleDateFormat.format(new Date());
            sqfwServicerecord.setCustomer_need(sqfwServicerecord.getType()+"任务【"+timeStr+"】");
        }else if (sqfwServicerecord.getType().equals("垃圾分类")){
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("MM-dd");
            String timeStr=simpleDateFormat.format(new Date());
            sqfwServicerecord.setCustomer_need(sqfwServicerecord.getType()+"任务【"+timeStr+"】");
        }else{
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("MM-dd");
            String timeStr=simpleDateFormat.format(new Date());
            sqfwServicerecord.setCustomer_need(sqfwServicerecord.getType()+"任务【"+timeStr+"】");
        }*/

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
        String timeStr = simpleDateFormat.format(new Date());
        sqfwServicerecord.setCustomer_need(sqfwServicerecord.getType() + "任务【" + timeStr + "】");

        Sqfw_servicerecord newServiceOrder = sqfwServicerecordService.insert(sqfwServicerecord);
        Result result = Result.NEW();
        result.addData(sqfwServicerecord.getId());

        //if (sqfwServicerecord.getService_phone()==null){
        //}
        // 新增语音通话需要生成一个工单
        Sqfw_workorder workOrder = new Sqfw_workorder();
        workOrder.setSqfw_id(newServiceOrder.getId());
        if (StringUtils.isNotBlank(newServiceOrder.getCreatedBy())) {
            Sys_user sysUser = sysUserService.fetch(newServiceOrder.getCreatedBy());
            if (sysUser != null) workOrder.setUser_name(sysUser.getUsername());
        }
        workOrder.setWk_grade("0");
        workOrder.setWk_priority("take_time_complete");
        workOrder.setUser_id(newServiceOrder.getCreatedBy());
        workOrder.setCreateDate(new Date());
        workOrder.setUpdatedAt(new Date());
        workOrder.setCreatedBy(newServiceOrder.getCreatedBy());
        // wzy 2023-5-25 服务记录生成工单的时候不主动完成对应工单
        workOrder.setWk_status("new");
        // workOrder.setComplete_time(new Date());
        workOrder.setDelflag(false);
        workOrder.setServiceType(newServiceOrder.getType());
        sqfwWorkorderService.insert(workOrder);

        /*if (StringUtils.isNotBlank(getPhone)) {
            List<String> strings = new ArrayList<>();
            //TransferServiceRecordDTO dto = new TransferServiceRecordDTO();
            transferServiceRecordDTO.setServiceRecordId(newServiceOrder.getId());
            List<String> list = Lists.newArrayList(getPhone.split(","));
            for (String str : list) {
                Sys_user sysUser = sysUserService.getUserByLoginname(str);
                strings.add(sysUser.getId());
            }
            transferServiceRecordDTO.setToSysUserIdList(strings);*/
        transferServiceRecordDTO.setServiceRecordId(newServiceOrder.getId());
        sqfwWorkorderService.transferServiceRecordNew(transferServiceRecordDTO);
        //}
        if (sqfwServicerecord.getApp_type().equals("8")) {
            Cnd cnd = Cnd.NEW();
            cnd.and("id", "=", ImgId);
            Sqfw_ImgCallback sqfwImgcallback = sqfwImgcallbackService.fetch(cnd);
            Sqfw_ImgCallback sqfw_imgCallback = new Sqfw_ImgCallback();
            sqfw_imgCallback.setImgUrl(sqfwImgcallback.getImgUrl());
            sqfw_imgCallback.setText(sqfwImgcallback.getText());
            sqfw_imgCallback.setCreatedBy(sqfwImgcallback.getCreatedBy());
            sqfw_imgCallback.setCreatedAt(sqfwImgcallback.getCreatedAt());
            sqfw_imgCallback.setUpdatedBy(sqfwImgcallback.getUpdatedBy());
            sqfw_imgCallback.setUpdatedAt(sqfwImgcallback.getUpdatedAt());
            sqfw_imgCallback.setDelFlag(false);
            sqfw_imgCallback.setBusinessId(sqfwImgcallback.getBusinessId());
            sqfw_imgCallback.setBusinessType(sqfwImgcallback.getBusinessType());

            sqfwImgcallbackService.insert(sqfw_imgCallback);
        }

        //SqfwCopyRecordMultiDTO multiDTO=new SqfwCopyRecordMultiDTO();
       /* if (StringUtils.isNotBlank(sqfwServicerecord.getCopyPhone())){
            Sys_user sysUser=sysUserService.getUserByLoginname(sqfwServicerecord.getCustomer_phone());

            List<String> listPhone= Arrays.stream(sqfwServicerecord.getCopyPhone().split(",")).collect(Collectors.toList());
            List<String> copyIds=new ArrayList<>();
            for (String str:listPhone) {
                Sys_user sysUserCopys=sysUserService.getUserByLoginname(str);
                copyIds.add(sysUserCopys.getId());
            }

            multiDTO.setSqfwId(newServiceOrder.getId());
            multiDTO.setCopyRemark("");
            multiDTO.setCopyUserid(sysUser.getId());
            multiDTO.setToUserIds(copyIds);
        }
        sqfwCopyrecordService.createMultiNew(multiDTO);*/

        return result;
    }
}
